//This class is created to perform type conversions.
//The code is from http://www.parashift.com/c++-faq-lite/

#ifndef CONVERT_H_
#define CONVERT_H_


#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <stdexcept>

class BadConversion : public std::runtime_error {
public:
	BadConversion(const std::string& s) : std::runtime_error(s) {}
};

template<typename T>
inline void convert(const std::string& s, T& x, bool failIfLeftoverChars = true) {
	std::istringstream i(s);
	char c;
	if(!(i>>x) || (failIfLeftoverChars && i.get(c)))
		throw BadConversion(s);
}

template<typename T>
inline T convertTo(const std::string& s, bool failIfLeftoverChars = true) {
	T x;
	convert(s,x, failIfLeftoverChars);
	return x;
}

template<typename T>
inline std::string tostring(const T& x) {
	std::ostringstream o;
        o.precision(12);
	if(!(o<<x))
		throw BadConversion(std::string("tostring(") + typeid(x).name() +")");
	return o.str();
}
template<class out_type, class in_value> out_type convert_any(const in_value &T) {
	std::stringstream ss;
	ss << T;
	out_type result;
	ss >> result;
	return result;
}
#endif /*CONVERT_H_*/
